﻿package com.ease.flex.remoting
{
	import com.ease.util.RemoteService;
	import com.ease.util.ServiceUtil;
	import com.ease.util.events.MethodReturnEvent;
	import com.ease.framework.CallbackUtil;
	import com.obas.flex.common.event.SessionTimeoutEvent;
	import com.obas.flex.common.util.ClientErrorUtil;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;

	import mx.managers.BrowserManager;
	import com.ease.platform.access.service.DatabaseColumnAccessService;
	import com.ease.platform.access.service.DatabaseManyToManyConstraintAccessService;
	import com.ease.platform.access.service.DatabaseManyToOneConstraintAccessService;
	import com.ease.platform.access.service.DatabaseTableAccessService;
	import com.ease.platform.cache.service.CacheManagementService;
	import com.ease.platform.cache.service.EnumerableModelManagementService;

	public class ServiceManagerComEasePlatformFlexRemoting {

		private static var _instance:ServiceManagerComEasePlatformFlexRemoting = null;

		public function ServiceManagerComEasePlatformFlexRemoting(singleton:SingletonClass) {
			var contextPath:String = ExternalInterface.call("BrowserHistory.getURL");
			RemoteService.defaultChannelUri = ServiceUtil.getDefaultChannelUri(contextPath);
		}

		public static function onOperationReturn(eventDispatcher:EventDispatcher, event:MethodReturnEvent):void {
	    	//var response:Event = null;
			//response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
			//eventDispatcher.dispatchEvent(response);
			
			var response:Event = null;
         	//Session timeout
         	if( null != event.error && ClientErrorUtil.getErrorType(event.error) == ClientErrorUtil.AUTHENTICATION_ERROR) {
            	var sessionTimeoutEvent:SessionTimeoutEvent = new SessionTimeoutEvent(SessionTimeoutEvent.SESSION_TIMEOUT);
            	eventDispatcher.dispatchEvent(sessionTimeoutEvent);
            	response = CallbackUtil.createResponseFromRequest(event.callContext, null, null);
         	} else {
            	response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
         	}
         	eventDispatcher.dispatchEvent(response);
		}

	    public static function getInstance():ServiceManagerComEasePlatformFlexRemoting {
	        if (_instance == null) {
				_instance = new ServiceManagerComEasePlatformFlexRemoting(new SingletonClass());
	        }

	        return _instance;
	    }
		private var _databaseColumnAccessService:DatabaseColumnAccessService = null;

		public static function getDatabaseColumnAccessService():DatabaseColumnAccessService {
			if (getInstance()._databaseColumnAccessService == null) {
				getInstance()._databaseColumnAccessService = new DatabaseColumnAccessService();
			}
			return getInstance()._databaseColumnAccessService;
		}

		public function set enabledDatabaseColumnAccessService(value:Boolean):void {
			if (getInstance()._databaseColumnAccessService) {
				getInstance()._databaseColumnAccessService.enabled = value;
			}
		}
		private var _databaseManyToManyConstraintAccessService:DatabaseManyToManyConstraintAccessService = null;

		public static function getDatabaseManyToManyConstraintAccessService():DatabaseManyToManyConstraintAccessService {
			if (getInstance()._databaseManyToManyConstraintAccessService == null) {
				getInstance()._databaseManyToManyConstraintAccessService = new DatabaseManyToManyConstraintAccessService();
			}
			return getInstance()._databaseManyToManyConstraintAccessService;
		}

		public function set enabledDatabaseManyToManyConstraintAccessService(value:Boolean):void {
			if (getInstance()._databaseManyToManyConstraintAccessService) {
				getInstance()._databaseManyToManyConstraintAccessService.enabled = value;
			}
		}
		private var _databaseManyToOneConstraintAccessService:DatabaseManyToOneConstraintAccessService = null;

		public static function getDatabaseManyToOneConstraintAccessService():DatabaseManyToOneConstraintAccessService {
			if (getInstance()._databaseManyToOneConstraintAccessService == null) {
				getInstance()._databaseManyToOneConstraintAccessService = new DatabaseManyToOneConstraintAccessService();
			}
			return getInstance()._databaseManyToOneConstraintAccessService;
		}

		public function set enabledDatabaseManyToOneConstraintAccessService(value:Boolean):void {
			if (getInstance()._databaseManyToOneConstraintAccessService) {
				getInstance()._databaseManyToOneConstraintAccessService.enabled = value;
			}
		}
		private var _databaseTableAccessService:DatabaseTableAccessService = null;

		public static function getDatabaseTableAccessService():DatabaseTableAccessService {
			if (getInstance()._databaseTableAccessService == null) {
				getInstance()._databaseTableAccessService = new DatabaseTableAccessService();
			}
			return getInstance()._databaseTableAccessService;
		}

		public function set enabledDatabaseTableAccessService(value:Boolean):void {
			if (getInstance()._databaseTableAccessService) {
				getInstance()._databaseTableAccessService.enabled = value;
			}
		}
		private var _cacheManagementService:CacheManagementService = null;

		public static function getCacheManagementService():CacheManagementService {
			if (getInstance()._cacheManagementService == null) {
				getInstance()._cacheManagementService = new CacheManagementService();
			}
			return getInstance()._cacheManagementService;
		}

		public function set enabledCacheManagementService(value:Boolean):void {
			if (getInstance()._cacheManagementService) {
				getInstance()._cacheManagementService.enabled = value;
			}
		}
		private var _enumerableModelManagementService:EnumerableModelManagementService = null;

		public static function getEnumerableModelManagementService():EnumerableModelManagementService {
			if (getInstance()._enumerableModelManagementService == null) {
				getInstance()._enumerableModelManagementService = new EnumerableModelManagementService();
			}
			return getInstance()._enumerableModelManagementService;
		}

		public function set enabledEnumerableModelManagementService(value:Boolean):void {
			if (getInstance()._enumerableModelManagementService) {
				getInstance()._enumerableModelManagementService.enabled = value;
			}
		}

	}
}
class SingletonClass{}
